package cn.myjerry.others.guava;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.common.collect.ImmutableList;

/**
 * 只读设置
 * @author Jerry
 *
 */
public class Demo01 {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("a");
		list.add("b");
		list.add("c");
		//对原有list进行包装，相等于原有list的一个视图，快照，不够安全
		List<String> readList = Collections.unmodifiableList(list);
		
		//readList.add("d");
		System.out.println(readList.size());
		list.add("d");	//改变原有list，视图一起改变
		System.out.println(readList.size());
		
		//对比查看 初始化list guava对只读设置安全可靠，并且相对简单
		List<String> immutablelist = ImmutableList.of("a", "b", "c");
		immutablelist.add("d");
		System.out.println(immutablelist.size());
	}
}
